const mongoose=require("mongoose");

const db= mongoose.createConnection('mongodb://localhost:27017/user',{useNewUrlParser:true, useUnifiedTopology: true});

mongoose.Promise = global.Promise;
db.on('error',(err)=>{console.log(err)});
db.on('open',()=>{console.log('mongodb连接成功')});
const Schema = mongoose.Schema;   //得到Schema构造函数
const UserShcema = new Schema({   //用Schema定义插入数据的类型
        name:{
            type:String,
            required:true
        },
        age:Number,
        sex:{
            type:String,
            required:true
        },
        address:{
            type:String,
            required:true
        },
        tel:{
            type:String,
            required:true
        }

 });
//建立集合，规定使用定义好的Shcema标准插入数据，返回一个构造函数
const Users = db.model('user',UserShcema,"user");
 //将Users暴露出去，可以用于操作数据
exports.Users = Users;